package com.home.qh.webapp.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.home.qh.model.EquipmentType;
import com.home.qh.service.EquipmentTypeManager;

@Controller
@RequestMapping("/admin/equipmentTypeForm.*")
public class EquipmentTypeFormController extends BaseFormController {
	private transient final Log log = LogFactory.getLog(EquipmentTypeFormController.class);
	private EquipmentTypeManager mgr;
    
    @Autowired
    private ApplicationContext context;

    @Autowired
    public void setEquipmentTypeManager(EquipmentTypeManager equipmentTypeManager) {
        this.mgr = equipmentTypeManager;
    }	
        
    public EquipmentTypeFormController() {
        setCancelView("redirect:/admin/equipmentTypeList.html");
        setSuccessView("redirect:/admin/equipmentTypeList.html");
    }
    @ModelAttribute
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    protected EquipmentType showForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	log.debug("entering 'showForm' method...");
    	
        if (!isFormSubmission(request)) {
            String equipmentTypeId = request.getParameter("id"); 
            EquipmentType equipmentType = null;
	        if (equipmentTypeId == null || isAdd(request)) {
	        	equipmentType = new EquipmentType();
	        } else if (!StringUtils.isBlank(equipmentTypeId)) {
	        	equipmentType = mgr.get(Long.parseLong(equipmentTypeId));
	        }
	        return equipmentType;
        }
    	return new EquipmentType();
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(EquipmentType equipmentType, BindingResult errors, HttpServletRequest request,
                           HttpServletResponse response)
            throws Exception {
        if (request.getParameter("cancel") != null) {
            if (!StringUtils.equals(request.getParameter("from"), "list")) {
                return getCancelView();
            } else {
                return getSuccessView();
            }
        }


        log.debug("entering 'onSubmit' method...");

        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
        	mgr.remove(equipmentType.getId());
            return getSuccessView();
        } else {
        	if (equipmentType == null)
        		equipmentType = new EquipmentType();   
        	log.debug("Save equipmentType");
        	System.out.println("Toi day nheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee: " + equipmentType.getCode() + " || " + equipmentType.getName());
        	mgr.save(equipmentType);
            return getSuccessView();        	
        }
    }

    private boolean isFormSubmission(HttpServletRequest request) {
        return request.getMethod().equalsIgnoreCase("post");
    }

    protected boolean isAdd(HttpServletRequest request) {
        String method = request.getParameter("method");
        return (method != null && method.equalsIgnoreCase("add"));
    }  

}
